package com.jy.novel.service;

import com.jy.novel.common.CommonResult;
import com.jy.novel.common.Page;
import com.jy.novel.dao.NovelChapterDao;
import com.jy.novel.pojo.NovelChapter;
import com.jy.novel.pojo.form.NovelChapterAddForm;
import com.jy.novel.pojo.form.NovelChapterEditForm;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by jihb on 4/28/16.
 */
@Service
public class NovelChapterService {

    @Autowired
    NovelChapterDao chapterDao;
    /**
     * 查询小说章节列表 分页
     * @param curPage
     * @param pageSize
     * @return
     */
    public CommonResult<Page<NovelChapter>> findNovelChapterPage(long novelId, int curPage, int pageSize){
        return CommonResult.returnDataWrap(chapterDao.findNovelChapterPage(novelId,curPage,pageSize));
    }

    /**
     *  查询小说章节列表 分页
     * @param novelId
     * @param curPage
     * @param pageSize
     * @return
     */
    public CommonResult<List<NovelChapter>> findNovelChapterList(long novelId, int curPage, int pageSize){
        return CommonResult.returnDataWrap(chapterDao.findNovelChapterList(novelId,curPage,pageSize));
    }

    /**
     * 根据id查询分类
     * @param id
     * @return
     */
    public CommonResult<NovelChapter> findNovelChapter(long id){
        return CommonResult.returnDataWrap(chapterDao.findNovelChapter(id));
    }

    /**
     * 添加小说章节
     * @param addForm
     * @return
     */
    public CommonResult addNovelChapter(NovelChapterAddForm addForm){
        if(addForm==null){
            return CommonResult.returnFailure("缺少参数");
        }
        if (StringUtils.isBlank(addForm.getName())){
            return CommonResult.returnFailure("缺少章节名称");
        }
        if (StringUtils.isBlank(addForm.getContent())){
            return CommonResult.returnFailure("缺少章节内容");
        }
        if (addForm.getNovelId()==null || addForm.getNovelId() < 1){
            return CommonResult.returnFailure("缺少所属小说");
        }
        if (addForm.getChapternum() == null){
            return CommonResult.returnFailure("缺少章节序号");
        }
        if (addForm.getChapternum() < 1){
            return CommonResult.returnFailure("章节序号错误");
        }
        return CommonResult.returnBoolWrap(chapterDao.addNovelChapter(addForm),"添加章节失败");
    }

    /**
     * 编辑小说章节
     * @param editForm
     * @param id
     * @return
     */
    public CommonResult updateNovelChapter(NovelChapterEditForm editForm, long id){
        if(editForm == null){
            return CommonResult.returnFailure("缺少参数");
        }
        if (id<1){
            return CommonResult.returnFailure("章节id错误");
        }
        return CommonResult.returnBoolWrap(chapterDao.updateNovelChapter(editForm,id),"修改章节失败");
    }

    /**
     * 删除章节
     * @param id
     * @return
     */
    public CommonResult deleteNovelChapter(long id){
        if(id<1){
            return CommonResult.returnFailure("章节id错误");
        }
        return CommonResult.returnBoolWrap(chapterDao.deleteNovelChapter(id),"删除章节失败");
    }
}
